/*
* Implement regular expressions
*/

#include "../inc/regex_digit.h"

/* initialize regular expression */
int init_regex(regex_t *reg, const char* pattern){
    if (regcomp(reg, pattern, REG_EXTENDED|REG_ICASE) != 0) {
        return 1; // error
    }else{
        return 0;
    }
}

/* use regual expression to match char or string */
int regex_match(regex_t *reg, char* buf, const size_t nmatch, regmatch_t pmatch[]){
    int status = regexec(reg, buf, nmatch, pmatch, 0);
    if (status == 0){
        return pmatch[0].rm_eo - pmatch[0].rm_so;
    }
    return 0;
}

/* free the regual expression */
void clear_regex(regex_t *reg){
    regfree(reg);
}
